package com.a1anwang.okble.beacon;

import com.a1anwang.okble.common.LogUtils;
import com.a1anwang.okble.common.OKBLEDataUtils;
import com.a1anwang.okble.server.advertise.OKBLEAdvertiseCallback;
import com.a1anwang.okble.server.advertise.OKBLEAdvertiseData;
import com.a1anwang.okble.server.advertise.OKBLEAdvertiseManager;
import com.a1anwang.okble.server.advertise.OKBLEAdvertiseSettings;
import ohos.app.Context;

/**
 * Created by a1anwang.com on 2018/6/1.
 */
public class OKBLEBeBeaconManager {
    private Context mContext;
    private OKBLEAdvertiseManager advertiseManager;
    private OKBLEStartBeaconListener listener;

    public void setOKBLEBeBeaconListener(OKBLEStartBeaconListener listener) {
        this.listener = listener;
    }

    /**
     * 构造函数
     *
     * @param context 上下文
     */
    public OKBLEBeBeaconManager(Context context) {
        this.mContext = context;
        init();
    }

    private void init() {
        advertiseManager = new OKBLEAdvertiseManager(mContext);
    }

    public void startIBeacon(String uuid, int major, int minor) {
        OKBLEAdvertiseSettings advertiseSettings = new OKBLEAdvertiseSettings.Builder().setConnectable(false).build();
        OKBLEAdvertiseData.Builder dataBuilder = new OKBLEAdvertiseData.Builder();

        // 按照apple iBeacon协议
        String beaconType = "0215";
        uuid = uuid.replace("-", "");
        String majorStr = OKBLEDataUtils.formatStringLenth(4, Integer.toHexString(major), '0');
        String minorStr = OKBLEDataUtils.formatStringLenth(4, Integer.toHexString(minor), '0');

        // -59是 measuredPower,一般设备默认都是-59，这里固定了
        String measuredPower = OKBLEDataUtils.formatStringLenth(2, Integer.toHexString(-59), '0');
        LogUtils.e(" measuredPower:" + measuredPower + "   " + Integer.toHexString(-59));
        String dataStr = beaconType + uuid + majorStr + minorStr + measuredPower;
        LogUtils.e(" dataStr:" + dataStr);
        byte[] data = OKBLEDataUtils.hexStringToBytes(dataStr);

        // 004c是apple 公司id
        dataBuilder.addManufacturerData(0x004C, data);
        advertiseManager.startAdvertising(advertiseSettings, dataBuilder.build(), new OKBLEAdvertiseCallback() {
            @Override
            public void onStartSuccess() {
                if (listener != null) {
                    listener.onStartSuccess();
                }
            }

            @Override
            public void onStartFailure(int errorCode, String errMsg) {
                if (listener != null) {
                    listener.onStartFailure(errMsg);
                }
            }
        });
    }

    /**
     * 停止函数
     */
    public void stop() {
        advertiseManager.stopAdvertising();
    }

    /**
     * 监听
     */
    public interface OKBLEStartBeaconListener {
        /**
         * 成功
         */
        void onStartSuccess();

        /**
         * 失败
         *
         * @param errMsg 错误代码
         */
        void onStartFailure(String errMsg);
    }
}
